//
//  RootTextView.m
//  FitOne
//
//  Created by saxon on 15/10/15.
//  Copyright (c) 2015年 jjian. All rights reserved.
//

#import "RTRootTextView.h"
#import <UIKit/UIKit.h>

@interface RTRootTextView ()

@property (nonatomic,strong) UILabel *placeholderLabel;
@end

@implementation RTRootTextView

- (void)keyChange:(NSNotification *)sender
{
    UITextView *textView = (UITextView *)sender.object;
    self.placeholderLabel.text = textView.text.length > 0 ? @"" : _placeholder;
    
    
    NSString *lang = [[[UITextInputMode activeInputModes] firstObject] primaryLanguage];//当前的输入模式
    if ([lang isEqualToString:@"zh-Hans"])
    {
        UITextRange *range = [textView markedTextRange];
        UITextPosition *start = range.start;
        UITextPosition*end = range.end;
        NSInteger selectLength = [textView offsetFromPosition:start toPosition:end];
        NSInteger contentLength = textView.text.length - selectLength;
        
        if (contentLength > _maxCount)
        {
            textView.text = [textView.text substringToIndex:_maxCount];
        }
    }
    else
    {
        if (textView.text.length > _maxCount)
        {
            textView.text = [textView.text substringToIndex:_maxCount];
            
        }
    }
    
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(textWillChange:)]) {
        [self.customDelegate textWillChange:self];
    }
}
- (void)setPlaceholder:(NSString *)placeholder
{
    
    _placeholder = placeholder;
    self.placeholderLabel.text = !self.text.length ? placeholder : @"";
    self.placeholderLabel.font = self.font;
    CGRect frame = self.placeholderLabel.frame;
    CGSize size = [self getTextHeight:placeholder font:self.font width:CGRectGetWidth(self.frame)-6];
    frame.size.height = size.height;
    self.placeholderLabel.frame = frame;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyChange:) name:UITextViewTextDidChangeNotification object:self];
}
- (UILabel *)placeholderLabel
{
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] init];
        _placeholderLabel.frame = CGRectMake(3, 8, CGRectGetWidth(self.frame) -6, 20);
        _placeholderLabel.font = self.font;
        _placeholderLabel.enabled = NO;//lable必须设置为不可用
        _placeholderLabel.backgroundColor = [UIColor clearColor];
        _placeholderLabel.numberOfLines = 0;
        [self addSubview:_placeholderLabel];
    }
    return _placeholderLabel;
}
- (void)setText:(NSString *)text
{
    [super setText:text];
    self.placeholderLabel.text = text.length > 0 ? @"" : _placeholder;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}
-(CGSize)getTextHeight:(NSString *)text font:(UIFont *)font width:(CGFloat)width
{
    return [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font ? font : [UIFont systemFontOfSize:15]} context:nil].size;
}
@end
